
using System;
using Gtk;

namespace GtkControls
{
    /// <summary>
    /// Extension methods for Gtk.MenuBar.
    /// </summary>
    static class GtkMenuBarExtension
    {
        /// <summary>
        /// Append a submenu to menu.
        /// </summary>
        public static Gtk.Menu AppendSubMenu(this Gtk.MenuBar menu,
                                           string label)
        {
            Gtk.MenuItem menuItem = menu.AppendMenuItem(label, delegate {});
            Gtk.Menu submenu = new Gtk.Menu();
            menuItem.Submenu = submenu;
            return submenu;
        }
        
        /// <summary>
        /// Append a menu item to the specified menu.
        /// </summary>
        public static Gtk.MenuItem AppendMenuItem(this Gtk.MenuBar menu,
                                           string label, EventHandler handler)
        {
            return AppendMenuItem (menu, label, handler, true);
        }
    
        /// <summary>
        /// Append a menu item to the specified menu.
        /// </summary>
        public static Gtk.MenuItem AppendMenuItem(this Gtk.MenuBar menu, string label,
                           EventHandler handler, bool enabled)
        {
            Gtk.MenuItem menuItem;
            Gtk.StockItem item = Gtk.StockItem.Zero;
            
            if (Gtk.StockManager.Lookup(label, ref item))
                menuItem = new Gtk.ImageMenuItem(label, new Gtk.AccelGroup());
            else
                menuItem = new Gtk.MenuItem(label);

            menuItem.Activated += handler;
            menuItem.Sensitive = enabled;
            
            menu.Append(menuItem);
            menuItem.Show();
            return menuItem;
        }

        public static Gtk.MenuItem AppendMenuItem(this Gtk.MenuBar menu, string label,
                       string imageName, EventHandler handler,
                       bool enabled)
        {
            Gtk.ImageMenuItem item = new Gtk.ImageMenuItem(label);
            item.Activated += handler;
            item.Sensitive = enabled;
            item.Image = new Gtk.Image(imageName, Gtk.IconSize.Menu);

            menu.Append(item);
            item.Show();
            return item;
        }

        /// <summary>
        /// Append a separator to the specified menu.
        /// </summary>
        public static Gtk.MenuItem AppendMenuSeparator(this Gtk.MenuBar menu)
        {
            Gtk.SeparatorMenuItem item = new Gtk.SeparatorMenuItem();
            menu.Append(item);
            item.Show();
            return item;
        }
    }
}
